<?php
namespace app\common\logic;

use app\common\component\user\UserService;
use app\common\component\user_wallet\UserWalletService;
use app\common\component\help_get\HelpGetModel;
use app\common\component\help_get\HelpGetService;
use app\common\component\help_to\HelpToModel;
use app\common\component\help_to\HelpToService;

class HelpTo {
	/*
	 * 确认打款订单
	 */
	public function confirmHelpTo($help_to_id) {
		$helpToService = new HelpToService();
		$restulHelpTo = $helpToService -> getHelpTo($help_to_id);
		if($restulHelpTo['code'] !== 0) {
			return $restulHelpTo;
		}

		$helpGetService = new HelpGetService();
		$restulHelpGet = $helpGetService -> getHelpGet($restulHelpTo['data']['help_to'] -> help_get_id);
		if($restulHelpGet['code'] !== 0) {
			return $restulHelpGet;
		}

		$data = [
			'begin_time' => time(),
			'status' => 2
		];

		\think\Db::startTrans();
		try {
			$resultHelpTo = $helpToService -> update($data, $help_to_id);
			if($resultHelpTo['code'] !== 0) {
				throw new \Exception($resultHelpTo['message']);
			}

			$resultHelpGet = $helpGetService -> update($data, $restulHelpTo['data']['help_to'] -> help_get_id);
			if($resultHelpGet['code'] !== 0) {
				throw new \Exception($resultHelpGet['message']);
			}

			$resultHelpTo = (new HelpGet()) -> confirmHelpGet($restulHelpTo['data']['help_to'] -> help_get_id);
			if($resultHelpTo['code'] !== 0) {
				throw new \Exception($resultHelpTo['message']);
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			return [
				'code' => -1,
				'message' => $e -> getMessage()
			];
		}

		return [
			'code' => 0,
			'message' => '打款成功'
		];
	}

	/**
	 * 确认首单打款
	 */
	public function firstConfirmHelpTo($help_to_id) {
		$helpToService = new HelpToService();
		$restulHelpTo = $helpToService -> getHelpTo($help_to_id);
		if($restulHelpTo['code'] !== 0) {
			return $restulHelpTo;
		}

		$helpGetService = new HelpGetService();
		$restulHelpGet = $helpGetService -> getHelpGet($restulHelpTo['data']['help_to'] -> help_get_id);
		if($restulHelpGet['code'] !== 0) {
			return $restulHelpGet;
		}

		$data = [
			'begin_time' => time(),
			'status' => 2
		];

		\think\Db::startTrans();
		try {
			$resultHelpGet = $helpGetService -> update($data, $restulHelpTo['data']['help_to'] -> help_get_id);
			if($resultHelpGet['code'] !== 0) {
				throw new \Exception($resultHelpGet['message']);
			}

			$resultHelpTo = $helpToService -> update($data, $help_to_id);
			if($resultHelpTo['code'] !== 0) {
				throw new \Exception($resultHelpTo['message']);
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			return [
				'code' => -1,
				'message' => $e -> getMessage()
			];
		}

		return [
			'code' => 0,
			'message' => '打款成功'
		];
	}

	/*
	 * 打款超时处理
	 */
	public function overtimeHelpTo() {
		$currentTime = time();
		$helpTos = HelpToModel::where('status = 1 AND (begin_time + ' . getParam('HELP_TO_MONEY_OVERTIME') * 60 * 60 . ') < ' . time()) -> select();

		foreach($helpTos as $helpTo) {
			$helpGet = HelpGetModel::get(['help_get_id' => $helpTo -> help_get_id]);
			$helpGet -> status = 9;
			$helpGet -> begin_time = $currentTime;

			$helpTo -> status = 8;
			$helpTo -> begin_time = $currentTime;
			
			// 超时次数
			$overtimeNum = HelpToModel::where('status = 8 AND user_id = ' . $helpTo -> user_id) -> count();

			\think\Db::startTrans();
			try {
				if($helpTo -> save()) {
					if($helpGet -> save()) {
						switch($overtimeNum) {
							case 0:
								break;
							case 1:
								$resultUserWallet = (new UserWalletService) -> setDecMoney($helpTo -> money / 2, $helpTo -> user_id);
								break;
							case 2:
								$resultUserWallet = (new UserWalletService) -> setDecMoney($helpTo -> money, $helpTo -> user_id);
								break;
							default:
								$resultUserUpdate = (new UserService()) -> update(['status' => 4], $helpTo -> user_id);
								break;
						}
					}
				}
				\think\Db::commit();
			} catch(\Exception $e) {
				\think\Db::rollback();
			}
		}
	}

	/**
	 * 打款用户冻结处理
	 */
	public function freezeHelpTo($user_id) {
		$currentTime = time();

		$helpTos = HelpToModel::where('status in(0, 1, 2, 3) AND user_id = ' . $user_id) -> select();
		foreach($helpTos as $helpTo) {
			$helpTo -> status = 8;
			$helpTo -> begin_time = $currentTime;

			\think\Db::startTrans();
			try {
				if($helpTo -> save()) {
					if($helpTo -> help_get_id != 0) {
						$getHelpGet = HelpGetModel::get($helpTo -> help_get_id);
						$getHelpGet -> help_to_id = 0;
						$getHelpGet -> begin_time = $currentTime;
						$getHelpGet -> status = 9;

						$getHelpGet -> save();
					}
				}
				\think\Db::commit();
			} catch(\Exception $e) {
				\think\Db::rollback();
			}
		}
	}
}
?>